package com.itheima.controller;

import com.itheima.annotation.LogOperation;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理控制器
 */
@RestController
@Slf4j
@RequestMapping("/depts")
public class DeptController {
    @Autowired
    private DeptService deptService;

    public DeptController(){
        System.out.println("DeptController 创建了 ....");
    }

    /**
     * 部门列表数据查询
     * @return
     */
    //RequestMapping 请求映射
    @GetMapping
    public Result list(){
       List<Dept> deptList =deptService.finAll();
     return Result.success(deptList);
    }

    /**
     * 删除部门
     * @param id
     * @return
     */
    @DeleteMapping
    public Result deleteById(Integer id){

        log.info("删除部门，id为：{}",id);
        deptService.deleteById(id);
        return Result.success();
    }


    /**
     * 添加部门
     * @param dept
     * @return
     */
    @LogOperation
    @PostMapping
    public Result add(@RequestBody Dept dept){
        log.info("添加部门，部门信息：{}",dept);
        deptService.add(dept);
        return Result.success();

    }

    /**
     * 根据id查询部门详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("查询部门，部门id：{}",id);
        Dept dept =deptService.deptService(id);
        return Result.success(dept);
    }

    /**
     * 更新部门
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("更新部门"+dept);
        deptService.update(dept);
        return Result.success();

    }


}
